package com.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.gulimall.product.entity.CategoryBrandRelationEntity;
import com.gulimall.product.service.impl.BrandServiceImpl;
import com.gulimall.product.service.impl.CategoryBrandRelationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gulimall.product.entity.BrandEntity;
import com.gulimall.product.service.BrandService;
import com.common.utils.PageUtils;
import com.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌
 *
 * @author Rookie-886
 * @email ${email}
 * @date 2021-03-14 16:30:53
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Resource
    private BrandServiceImpl brandServiceImpl;

    @Resource
    private CategoryBrandRelationServiceImpl categoryBrandRelationServiceImpl;
    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandServiceImpl.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
//    @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandServiceImpl.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 获取批量信息
     */
    @PostMapping("/infoList")
//    @RequiresPermissions("product:brand:info")
    public R infoList(@RequestParam("brandIdList") List<Long> brandIdList){
        List<BrandEntity> brandList = brandServiceImpl.listByIds(brandIdList);

        return R.ok().setData(brandList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:brand:save")
    public R save(@RequestBody BrandEntity brand){
        brandServiceImpl.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand){
        if(brand.getName() != null){
            CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
            categoryBrandRelationEntity.setBrandId(brand.getBrandId());
            categoryBrandRelationEntity.setBrandName(brand.getName());
            categoryBrandRelationServiceImpl.updateName(categoryBrandRelationEntity);
        }
        brandServiceImpl.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
        brandServiceImpl.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
